package cn.outsourcing.supermarket.service.impl;

import cn.outsourcing.supermarket.common.CommonPage;
import cn.outsourcing.supermarket.common.PageResult;
import cn.outsourcing.supermarket.common.constants.CommonConstants;
import cn.outsourcing.supermarket.common.exception.SupermarketException;
import cn.outsourcing.supermarket.common.utils.IPageUtils;
import cn.outsourcing.supermarket.common.utils.PageResultUtils;
import cn.outsourcing.supermarket.converter.HiolabsAdConverter;
import cn.outsourcing.supermarket.dto.OnlyIdPayloadDTO;
import cn.outsourcing.supermarket.dto.backstage.BackstageAdInfoDTO;
import cn.outsourcing.supermarket.dto.backstage.BackstageUpdateStatusDTO;
import cn.outsourcing.supermarket.mapper.HiolabsAdMapper;
import cn.outsourcing.supermarket.mapper.HiolabsGoodsMapper;
import cn.outsourcing.supermarket.po.HiolabsAd;
import cn.outsourcing.supermarket.po.HiolabsGoods;
import cn.outsourcing.supermarket.service.HiolabsAdService;
import cn.outsourcing.supermarket.vo.backstage.BackstageAdInfoVO;
import cn.outsourcing.supermarket.vo.backstage.BackstageRelateVO;
import cn.outsourcing.supermarket.vo.hiolabs.HiolabsAdVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 广告表 服务实现类
 *
 * @author gzkemays
 * @since 2021-10-22
 */
@Service
public class HiolabsAdServiceImpl extends ServiceImpl<HiolabsAdMapper, HiolabsAd>
    implements HiolabsAdService {
  @Resource HiolabsAdMapper adMapper;
  @Resource HiolabsGoodsMapper goodsMapper;

  @Override
  public PageResult<BackstageAdInfoVO> getAdListPageList(CommonPage page) {
    IPageUtils<HiolabsAd> properties = IPageUtils.<HiolabsAd>init().properties(page);
    return PageResultUtils.parseListPageObjs(
        properties,
        HiolabsAdConverter.INSTANCE.converterToBackstageVoList(
            adMapper
                .selectPage(
                    properties,
                    Wrappers.<HiolabsAd>lambdaQuery()
                        .eq(HiolabsAd::getIsDelete, 0)
                        .orderByAsc(HiolabsAd::getSortOrder))
                .getRecords()));
  }

  @Override
  public String saleStatus(BackstageUpdateStatusDTO dto) {
    HiolabsAd hiolabsAd = new HiolabsAd();
    hiolabsAd.setId(dto.getId());
    hiolabsAd.setEnabled(dto.getStatus());
    if (adMapper.updateById(hiolabsAd) > 0) {
      return "OK";
    }
    throw new SupermarketException(CommonConstants.SQL_FAIL_CODE, "更新失败");
  }

  @Override
  public HiolabsAdVO info(OnlyIdPayloadDTO dto) {
    return HiolabsAdConverter.INSTANCE.converterToVo(adMapper.selectById(dto.getId()));
  }

  @Override
  public HiolabsAdVO store(BackstageAdInfoDTO dto) {
    HiolabsAd hiolabsAd = HiolabsAdConverter.INSTANCE.converterBackstageAdInfoToPo(dto);
    String goodsId = dto.getGoodsId();
    if (goodsMapper.selectById(goodsId) == null) {
      throw new SupermarketException(CommonConstants.REQUEST_FAIL_CODE, "没有该商品，请前往商品列表确认");
    }
    if (adMapper.selectOne(Wrappers.<HiolabsAd>lambdaQuery().eq(HiolabsAd::getGoodsId, goodsId))
        != null) {
      throw new SupermarketException(100, "该商品已被绑定");
    }
    if (dto.getId().equals(0)) {
      hiolabsAd.setId(null);
    }
    saveOrUpdate(hiolabsAd);
    return HiolabsAdConverter.INSTANCE.converterToVo(hiolabsAd);
  }

  @Override
  public PageResult<BackstageRelateVO> getRelates() {
    IPageUtils<HiolabsGoods> hiolabsGoodsIPageUtils =
        IPageUtils.<HiolabsGoods>init().page(1).limit(5).create();
    List<BackstageRelateVO> list = new ArrayList<>();
    List<HiolabsGoods> hiolabsGoods =
        goodsMapper
            .selectPage(
                hiolabsGoodsIPageUtils,
                Wrappers.<HiolabsGoods>lambdaQuery()
                    .select(
                        HiolabsGoods::getId, HiolabsGoods::getName, HiolabsGoods::getListPicUrl))
            .getRecords();
    for (HiolabsGoods hiolabsGood : hiolabsGoods) {
      BackstageRelateVO vo = new BackstageRelateVO();
      vo.setName(hiolabsGood.getName());
      vo.setListPicUrl(hiolabsGood.getListPicUrl());
      vo.setId(hiolabsGood.getId());
      list.add(vo);
    }
    return PageResultUtils.parseListPageObjs(hiolabsGoodsIPageUtils, list);
  }

  @Override
  public Integer destroyAd(OnlyIdPayloadDTO dto) {
    HiolabsAd ad = new HiolabsAd();
    ad.setId(dto.getId());
    ad.setIsDelete(1);
    return adMapper.updateById(ad);
  }
}
